import { BaseEntity } from "src/modules/common/entities/BaseEntity";
import { Role } from "src/modules/role/entities/role.entity";
import { Column, Entity, JoinColumn, ManyToMany, ManyToOne, OneToMany, OneToOne, Tree, TreeChildren, TreeParent } from "typeorm";
import { Meta } from "./meta.entity";
import { Permission } from "./menu.permission.entity";

@Entity()
@Tree("closure-table")
export class Menu extends BaseEntity {
  @Column()
  name: string;

  @Column()
  path: string;

  @Column()
  component: string;

  @Column({
    nullable: true
  })
  redirect: string;

  @Column({
    name: "meta_id",
  })
  metaId: string;

  @OneToOne(type => Meta, { cascade: true })
  @JoinColumn({
    name: "meta_id"
  })
  meta: Meta;

  @Column({
    type: 'varchar',
    nullable: true,
    default: null,
    name: 'parent_id',
    comment: '父级id',
    transformer: { from: (value) => value || '0', to: (value: string) => value },
  })
  parentId: string;

  @TreeChildren({
    cascade: true
  })
  children: Menu[];

  @TreeParent()
  @JoinColumn({
    name: "parent_id"
  })
  parent: Menu;

  @ManyToMany(() => Role, role => role.menu)
  @JoinColumn()
  role: Role[]

  @OneToMany(type => Permission, permission=>permission.menu, { cascade: true })
  permission: Permission[]

}